<?php
/**
 * Base model class
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single 
 * database entry.
 * 
 * @package    FireFeed
 * @subpackage Model
 */
abstract class Model_Entity_BaseBean
{
    /**
     * @var int Object ID
     */	
	protected $_oid;
	
    /**
     * Constructor
     * 
     * @param  array|null $options 
     * @return void
     */
    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }
    
    /**
     * Set object state
     * 
     * @param  array $options 
     * @return Model_Entity_BaseBean
     */
    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
    /**
     * Get object state as associative array
     * Return only simple properties
     *  
     * @return array associative array[property][value]
     */
    public function getOptions()
    {
        $toArray = array();
        
    	$classMethods = get_class_methods($this);
        $maskMethods = Array('__construct', '__set', '__get', 'setOptions', 
                             'getOptions', 'setMapper', 'getMapper');
        $methods = array_diff($classMethods, $maskMethods);
        
        foreach ($methods as $name) {
        	$init = substr($name,0,3);
        	if($init === 'get') {
        		$property = substr($name,3);
        		$property = strtolower(substr($property,0,1)).substr($property,1);
        		
        		if (!is_array($this->$property) && !is_object($this->$property)) {
        		  $val = $this->$name();
        		  $toArray =  array_merge($toArray, array($property => $val));
        		}
        	}
        }
        
        return $toArray;
    }
    
    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @param  mixed $value 
     * @return void
     */
    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if ('mapper' == $name || !method_exists($this, $method)) {
            throw new Exception('Invalid property specified');
        }
        $this->$method($value);
    }

    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @return mixed
     */
    public function __get($name)
    {
        $method = 'get' . $name;
        if ('mapper' == $name || !method_exists($this, $method)) {
            throw new Exception('Invalid property specified');
        }
        return $this->$method();
    }
    
    /**
     * 
     * @param $oid
     * @return void
     */
    public function setOid($oid)
    {
        $this->_oid = $oid;
    }
    
    /**
     * 
     * @return int
     */
    public function getOid()
    {
        return $this->_oid;
    }
    
}